package com.example.wangzh.developkit.widget;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.wangzh.developkit.R;
import com.example.wangzh.developkit.adapter.KeybordGridViewAdapter;
import com.example.wangzh.developkit.utils.UIUtils;
import com.example.wangzh.developkit.utils.ViewUtils;

/**
 * Created by SP on 2017/2/7.
 */

public class PasswordView implements View.OnClickListener {


    private ResultHandler handler;
    private Context context;
    protected Dialog passwordDialog;
    private GridView keyBordView;

    private String strPassword;     //输入的密码
    private TextView[] tvList;
    private ImageView imgCancel;
    private TextView tvForget;
    private int currentIndex = -1;    //用于记录当前输入密码格位置
    private KeybordGridViewAdapter kbAdapter;
    private TextView tv_show;
    protected TextView txt_title;

    protected boolean showTxtshow() {
        return true;
    }

    protected boolean showTxtForget() {
        return true;
    }

    public interface ResultHandler {
        void handle(String time);
    }

    public PasswordView(Context context, ResultHandler resultHandler) {

        this.context = context;
        this.handler = resultHandler;

        initDialog();
        initView();
    }


    private void initDialog() {

        if (passwordDialog == null) {
            passwordDialog = new Dialog(context, R.style.time_dialog);
            passwordDialog.setCancelable(true);
            passwordDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            passwordDialog.setContentView(R.layout.dialog_input_password);
            Window window = passwordDialog.getWindow();
            window.setGravity(Gravity.BOTTOM);
            window.setWindowAnimations(R.style.MenuDialogAnimation); // 添加动画
            WindowManager.LayoutParams lp = window.getAttributes();
            int width = UIUtils.getScreenWidth();
            lp.width = width;
            window.setAttributes(lp);
        }
    }


    private void initView() {

        tvList = new TextView[6];

        imgCancel = (ImageView) passwordDialog.findViewById(R.id.img_cancel);
        imgCancel.setOnClickListener(this);

        tvForget = (TextView) passwordDialog.findViewById(R.id.tv_forgetPwd);
        tvForget.setOnClickListener(this);
        ViewUtils.visibleOrInvisible(tvForget, showTxtForget());

        tv_show = (TextView) passwordDialog.findViewById(R.id.tv_show);
        ViewUtils.visibleOrInvisible(tv_show, showTxtshow());

        txt_title = (TextView) passwordDialog.findViewById(R.id.txt_title);

        tvList[0] = (TextView) passwordDialog.findViewById(R.id.tv_pass1);
        tvList[1] = (TextView) passwordDialog.findViewById(R.id.tv_pass2);
        tvList[2] = (TextView) passwordDialog.findViewById(R.id.tv_pass3);
        tvList[3] = (TextView) passwordDialog.findViewById(R.id.tv_pass4);
        tvList[4] = (TextView) passwordDialog.findViewById(R.id.tv_pass5);
        tvList[5] = (TextView) passwordDialog.findViewById(R.id.tv_pass6);

        keyBordView = (GridView) passwordDialog.findViewById(R.id.gv_keybord);
        setKeybord();
    }


    private void setKeybord() {

        kbAdapter = new KeybordGridViewAdapter(context);
        keyBordView.setAdapter(kbAdapter);

        keyBordView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                if (position < 11 && position != 9) {    //点击0~9按钮

                    if (currentIndex >= -1 && currentIndex < 5) {      //判断输入位置————要小心数组越界
                        String number = "";
                        if (position == 10) { //点击position == 10的按钮代表数字0
                            number = "0";
                        } else {
                            number = String.valueOf(position + 1); //当前的数字比position大1
                        }
                        tvList[++currentIndex].setText(String.valueOf(number));
                    }

                } else {
                    if (position == 11) {      //点击退格键
                        if (currentIndex - 1 >= -1) {      //判断是否删除完毕————要小心数组越界
                            tvList[currentIndex--].setText("");
                        }
                    }
                }

                if (currentIndex == 5) {//当输入有四个数字时候
                    strPassword = "";     //每次触发都要先将strPassword置空，再重新获取，避免由于输入删除再输入造成混乱
                    for (int i = 0; i < 6; i++) {
                        strPassword += tvList[i].getText().toString().trim();
                    }

                    handler.handle(strPassword);
                }
            }
        });
    }


    @Override
    public void onClick(View view) {

        switch (view.getId()) {

            case R.id.img_cancel:
                dismissDialog();
                break;

            case R.id.tv_forgetPwd:
//                passwordDialog.dismiss();
//				Intent intent = new Intent(context, ActForgetPayPwd.class);
//				context.startActivity(intent);
                break;

        }
    }


    public void show(String str) {

        if (passwordDialog != null && !passwordDialog.isShowing()) {
            tv_show.setText(str);
            passwordDialog.show();
        }

    }

    public void show() {
        if (passwordDialog != null && !passwordDialog.isShowing()) {
            txt_title.setText("请输入支付密码");
            passwordDialog.show();
        }
    }

    public void clearPassword() {

        for (int index = 0; index <= 5; index++) {
            tvList[index].setText("");
        }
        currentIndex = -1;
    }


    public void dismissDialog() {

        passwordDialog.dismiss();
    }

    public void setTitle(String s) {
        txt_title.setText(s);

    }

    public void setOnDismissListener(DialogInterface.OnDismissListener onDismissListener) {
        passwordDialog.setOnDismissListener(onDismissListener);
    }
}
